using System.Dynamic;
using System.Text.RegularExpressions;
using UniversalAdminSystem.Domian.Core;
using UniversalAdminSystem.Domian.Core.ValueObjects;
using UniversalAdminSystem.Domian.UserManagement.ValueObj;

namespace UniversalAdminSystem.Domian.UserManagement.Aggregates;

public partial class User : AggregateRoot
{
    public UserId UserId { get; private set; } = null!;
    public UserAccount Account { get; private set; } = null!;
    public string Password { get; private set; } = null!;
    public string Salt { get; private set; } = null!;
    public UserEmail Email { get; private set; } = null!;
    public UserStatus Status { get; private set; }
    public UserInfoId? UserInfoId { get; private set; }

    public RoleId? RoleId { get; private set; }

    private User() { }

    private User(
        UserInfoId userInfoId,
        UserAccount account,
        string password,
        UserEmail email,
        string salt,
        UserStatus userStatus,
        RoleId? role = null
    )
    {
        UserId = UserId.Create(Guid.NewGuid());
        UserInfoId = userInfoId;
        Account = account;
        Password = password;
        Email = email;
        Salt = salt;
        Status = userStatus;
        RoleId = role;
    }

    public static User CreateUser(UserInfoId userInfoId, string account, string password, string email, string salt, UserStatus userStatus = UserStatus.Normal, Guid? roleId = null)
    {
        var user = new User(
            userInfoId,
            UserAccount.Create(account),
            password,
            UserEmail.Create(email),
            salt,
            userStatus,
            roleId.HasValue ? RoleId.Create(roleId.Value) : null
        );

        return user;
    }

    public User UpdateUserEmail(string email)
    {
        Email = UserEmail.Create(email);
        return this;
    }

    public User UpdateUserPassword(string password, string salt)
    {
        if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("密码不得为空");
        if (string.IsNullOrWhiteSpace(salt)) throw new ArgumentException("盐值不得为空");
        Password = password;
        Salt = salt;
        return this;
    }

    public User UpdateUserStatus(UserStatus userStatus)
    {
        Status = userStatus;
        return this;
    }

    public User AssignRole(RoleId roleId)
    {
        if (RoleId != roleId)
        {
            RoleId = roleId;
        }
        return this;
    }

    public User RemoveRole(RoleId roleId)
    {
        if (RoleId == roleId)
        {
            RoleId = null;
        }
        return this;
    }

    public User ChangeStatus(UserStatus newStatus)
    {
        if (Status != newStatus)
        {
            var oldStatus = Status;
            Status = newStatus;
        }
        return this;
    }

    public User ChangePassword(string newPassword, string newSalt)
    {
        if (string.IsNullOrWhiteSpace(newPassword)) throw new ArgumentException("密码不得为空");
        Password = newPassword;
        Salt = newSalt;
        return this;
    }

    public User SoftDelete()
    {
        Status = UserStatus.Deleted;
        return this;
    }

    [GeneratedRegex(@"^[a-zA-Z][a-zA-Z0-9_]{5,19}$", RegexOptions.IgnoreCase, "zh-CN")]
    private static partial Regex MyRegex();
}